using System;
using System.Security.Principal;
using System.Web;
using System.Web.Caching;

namespace MyWebPagesStarterKit.Sitemaps.Utilities
{
    public abstract class Handler
    {
        private HttpContext _context;

        protected Handler()
        {
        }

        protected HttpApplicationState Application
        {
            get
            {
                if (this.Context != null)
                {
                    return this.Context.Application;
                }
                return null;
            }
        }

        protected HttpApplication ApplicationInstance
        {
            get
            {
                if (this.Context != null)
                {
                    return this.Context.ApplicationInstance;
                }
                return null;
            }
        }

        protected System.Web.Caching.Cache Cache
        {
            get
            {
                if (this.Context != null)
                {
                    return this.Context.Cache;
                }
                return null;
            }
        }

        protected HttpContext Context
        {
            get
            {
                if (this._context == null)
                {
                    this._context = HttpContext.Current;
                }
                return this._context;
            }
            set
            {
                this._context = value;
            }
        }

        protected HttpRequest Request
        {
            get
            {
                if (this.Context != null)
                {
                    return this.Context.Request;
                }
                return null;
            }
        }

        protected HttpResponse Response
        {
            get
            {
                if (this.Context != null)
                {
                    return this.Context.Response;
                }
                return null;
            }
        }

        protected HttpServerUtility Server
        {
            get
            {
                if (this.Context != null)
                {
                    return this.Context.Server;
                }
                return null;
            }
        }

        protected TraceContext Trace
        {
            get
            {
                if (this.Context != null)
                {
                    return this.Context.Trace;
                }
                return null;
            }
        }

        public IPrincipal User
        {
            get
            {
                if (this.Context != null)
                {
                    return this.Context.User;
                }
                return null;
            }
        }
    }
}
